﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace Refraction
{
    public class SnellsLaw : Actor
    {
        #region Variables 

        private TextureCube _cubeMap;
        public TextureCube DynamicCube;
        private KeyboardState _lastKeyboardState;
        private bool _isHLSLRefraction = false;
        #endregion

        public SnellsLaw(Game game, Vector3 position)
            : base(game, position)
        {  
            
        }

        protected override void LoadContent()
        {
            _effect = Game.Content.Load<Effect>("Shaders/SnellsLawEffect");
            _model = Game.Content.Load<Model>("Models/ball");
            _cubeMap = Game.Content.Load<TextureCube>("Textures/cubeMap");

            _effect.Parameters["CubeMap"].SetValue(_cubeMap);

            _lastKeyboardState = Keyboard.GetState(); 

            base.LoadContent();
        }

        public override void Update(GameTime gameTime)
        {
            #region Input
            KeyboardState currentKeyboardState = Keyboard.GetState();

            if (currentKeyboardState.IsKeyDown(Keys.H) && _lastKeyboardState.IsKeyUp(Keys.H))
                _isHLSLRefraction = !_isHLSLRefraction;   

            _lastKeyboardState = currentKeyboardState;
            #endregion

            _effect.Parameters["IOR"].SetValue(IOR);
            _effect.Parameters["View"].SetValue(Camera.view);
            _effect.Parameters["Projection"].SetValue(Camera.projection);
            _effect.Parameters["CameraPosition"].SetValue(Camera.position);
            _effect.Parameters["UseHLSLRefraction"].SetValue(_isHLSLRefraction); //if true, the shader uses the HLSL refract method to do refraction
        
            base.Update(gameTime);
        } 

        public override void Draw(GameTime gameTime)
        {
            if(DynamicCube == null)
                _effect.Parameters["CubeMap"].SetValue(_cubeMap);
            else
                _effect.Parameters["CubeMap"].SetValue(DynamicCube);
            Matrix world = Matrix.CreateScale(5f)*Matrix.CreateTranslation(Position);

            DrawModel(world);
        }
    }
}
